#include "addCourse.h"
#include "Settings.h"
#include <QtGui/QMessageBox>

AddCourse::AddCourse()
{
    this->setupUi(this);
    this->setWindowTitle(QString::fromUtf8("增加课程"));

    connect(this->bt_addcourse,SIGNAL(clicked()),this,SLOT(addCourse()));
    connect(this->bt_cancel,SIGNAL(clicked()),this,SLOT(close()));
}

void AddCourse::addCourse()
{
    QString classname=this->et_classname->text().trimmed();
    if(classname.isEmpty())
    {
        QMessageBox::warning(this,QString::fromUtf8("警告！"),QString::fromUtf8("课程名字不能为空"));
        return;
    }
    int classid;
    if(!this->et_classID->text().trimmed().isEmpty())
        classid=(this->et_classID->text()).toInt();
    else classid=-1;
    QString teacher=this->et_teacher->text().trimmed();
    QString classroom=this->et_classroom->text().trimmed();
    if(classroom.isEmpty())
    {
        QMessageBox::warning(this,QString::fromUtf8("警告！"),QString::fromUtf8("授课地点不能为空"));
        return;
    }
    if(this->et_startweek->text().trimmed().isEmpty())
    {
        QMessageBox::warning(this,QString::fromUtf8("警告！"),QString::fromUtf8("第几周开始选项不能为空"));
        return;
    }
    int weekstart=this->et_startweek->text().trimmed().toInt();
    if(this->et_endweek->text().trimmed().isEmpty())
    {
        QMessageBox::warning(this,QString::fromUtf8("警告！"),QString::fromUtf8("第几周结束选项不能为空"));
        return;
    }
    int weekend=this->et_endweek->text().trimmed().toInt();
    if(this->et_dayofweek->text().trimmed().isEmpty())
    {
        QMessageBox::warning(this,QString::fromUtf8("警告！"),QString::fromUtf8("周几的课选项不能为空"));
        return;
    }
    int weekday=this->et_dayofweek->text().trimmed().toInt();
    if(this->et_startcoursenumber->text().trimmed().isEmpty())
    {
        QMessageBox::warning(this,QString::fromUtf8("警告！"),QString::fromUtf8("从第几节开始选项不能为空"));
        return;
    }
    int coursenumber=this->et_startcoursenumber->text().trimmed().toInt();
    if(this->et_coursecount->text().trimmed().isEmpty())
    {
        QMessageBox::warning(this,QString::fromUtf8("警告！"),QString::fromUtf8("共有几节课选项不能为空"));
        return;
    }
    int coursecount=this->et_coursecount->text().trimmed().toInt();
    int credit=-1;
    if(!this->et_coursecredit->text().trimmed().isEmpty())
    {
        credit=this->et_coursecredit->text().trimmed().toFloat();
    }
    QString otherinfo=this->et_otherinfo->text().trimmed();

    //数据库写入
    QSqlDatabase db=QSqlDatabase::addDatabase("QMYSQL");//数据库查询
    db.setHostName(HOSTNAME);
    db.setUserName(USERNAME);
    db.setPassword(PASSWORD);
    db.setDatabaseName(DATABASENAME);
    if(!db.open())
    {
        QMessageBox::critical(0,QObject::tr("数据库出错"),db.lastError().text());
        return ;
    }
    QSqlQuery query;
    if(credit!=-1&&classid!=-1)
        query.exec("insert into classinfo values(\""+classname+"\","+QString::number(classid)+",\""+teacher+"\","+QString::number(weekstart)+","+QString::number(weekend)+",\""+classroom+"\","+QString::number(weekday)+","+QString::number(coursenumber)+","+QString::number(coursecount)+","+QString::number(credit)+",\""+otherinfo+"\")");
    else
        if(credit!=-1&&classid==-1)
            query.exec("insert into classinfo values(\""+classname+"\",NULL,\""+teacher+"\","+QString::number(weekstart)+","+QString::number(weekend)+",\""+classroom+"\","+QString::number(weekday)+","+QString::number(coursenumber)+","+QString::number(coursecount)+","+QString::number(credit)+",\""+otherinfo+"\")");
    else
        if(credit==-1&&classid!=-1)
            query.exec("insert into classinfo values(\""+classname+"\","+QString::number(classid)+",\""+teacher+"\","+QString::number(weekstart)+","+QString::number(weekend)+",\""+classroom+"\","+QString::number(weekday)+","+QString::number(coursenumber)+","+QString::number(coursecount)+",NULL,\""+otherinfo+"\")");
    else
        query.exec("insert into classinfo values(\""+classname+"\",NULL,\""+teacher+"\","+QString::number(weekstart)+","+QString::number(weekend)+",\""+classroom+"\","+QString::number(weekday)+","+QString::number(coursenumber)+","+QString::number(coursecount)+",NULL,\""+otherinfo+"\")");
    db.close();
    emit addcoursefinish();
    this->close();
}

AddCourse::~AddCourse()
{}
